package prepare;

/**
 * SynchronizedDemo
 *
 * @author <a href="zhaoyingling12@163.com">Zhao Simon</a>
 * @description:
 * @date 2021/6/2
 **/
public class SynchronizedDemo {

    public static Object lock = new Object();

    public static void main(String[] args) throws InterruptedException {
        Thread thread1 = new Thread(new Task(lock));
        Thread thread2 = new Thread(new Task(lock));
        thread1.start();
        Thread.sleep(100);
        thread2.start();
        System.out.println("主线程开始中断 thread2 ");
        thread2.interrupt();
        System.out.println(thread2.isInterrupted());
    }

    static class Task implements Runnable {

        private Object lock;

        public Task(Object lock) {
            this.lock = lock;
        }

        @Override
        public void run() {
            synchronized (lock) {
                System.out.println(Thread.currentThread().getName() + "开始执行");
                while (true) {

                }
            }
        }
    }
}
